package com.cr.crandroid.ui.appointment;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cr.crandroid.R;
import com.cr.crandroid.adapter.AppointmentAdapter;
import com.cr.crandroid.adapter.OrderAdapter;
import com.cr.crandroid.ui.person.user.LoginViewModel;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.utils.OConvertUtils;
import com.cr.crandroid.utils.OkHttpManager;
import com.cr.crandroid.vo.RentCarVo;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class OrderFragment extends Fragment {
    private OrderAdapter orderAdapter;
    private List<RentCarVo> rentCarVos = new ArrayList<>();
    private RentCarVo rentCarVo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_order, container, false);
        MaterialAlertDialogBuilder materialAlertDialog = new MaterialAlertDialogBuilder(getContext())
                .setTitle("完成订单");
        orderAdapter = new OrderAdapter(null, materialAlertDialog);
        materialAlertDialog.setNeutralButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                rentCarVo = orderAdapter.getRentCarVo();
                finish();
            }
        });
        materialAlertDialog.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                return;
            }
        });
        RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
        recyclerView.setAdapter(orderAdapter);
        View emptyCar = inflater.inflate(R.layout.empty_car, container, false);
        orderAdapter.setEmptyView(emptyCar);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        getRentCarVo();
        return root;
    }

    private void finish() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpManager.postJ(CommonConstant.URL + "/rent/finish", new Gson().toJson(rentCarVo), new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String res = response.body().string();
                        JsonObject result = JsonParser.parseString(res).getAsJsonObject();
                        if (Integer.parseInt(String.valueOf(result.get("code"))) != CommonConstant.ERROR) {
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    getRentCarVo();
                                    Snackbar.make(getView(), "收车成功",
                                            Snackbar.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
                });
            }
        }).start();
    }

    @Override
    public void onResume() {
        super.onResume();
        getRentCarVo();
    }

    private void getRentCarVo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, String> map = new HashMap<>();
                map.put("userId", OConvertUtils.getString(LoginViewModel.user.get("id").toString()));
                OkHttpManager.get(CommonConstant.URL + "/rent/queryPersonOrder", map, new Callback() {
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String res = response.body().string();
                        JsonObject result = JsonParser.parseString(res).getAsJsonObject();
                        if (Integer.parseInt(String.valueOf(result.get("code"))) != CommonConstant.ERROR) {
                            if (result.get("result") != null) {
                                rentCarVos = new Gson().fromJson(result.get("result"), new TypeToken<List<RentCarVo>>() {
                                }.getType());
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        orderAdapter.setList(rentCarVos);
                                    }
                                });

                            }
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }
                });
            }
        }).start();
    }
}